package com.flyco.modules.ums.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.ums.entity.UmsMemberSign;
import com.flyco.modules.ums.entity.UmsMemberSignLog;
import com.flyco.modules.ums.service.IUmsMemberSignLogService;
import com.flyco.modules.ums.service.IUmsMemberSignService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 会员签到
 * @Author: flyco
 * @Date: 2021-06-16
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "会员签到")
@RestController
@RequestMapping("/f/ums/umsMemberSign")
public class UmsMemberSignController extends BaseController<UmsMemberSign, IUmsMemberSignService> {
    @Autowired
    private IUmsMemberSignService umsMemberSignService;
	@Autowired
    private IUmsMemberSignLogService umsMemberSignLogService;

    /**
     * 分页列表查询
     *
     * @param umsMemberSign
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "会员签到-分页列表查询")
    @ApiOperation(value = "会员签到-分页列表查询", notes = "会员签到-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(UmsMemberSign umsMemberSign,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<UmsMemberSign> queryWrapper = QueryGenerator.initQueryWrapper(umsMemberSign, req.getParameterMap());
        Page<UmsMemberSign> page = new Page<UmsMemberSign>(pageNo, pageSize);
        IPage<UmsMemberSign> pageList = umsMemberSignService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过会员id查询签到日志
     *
     * @param umsMemberSignLog
     * @return
     */
    @AutoLog(value = "门店库存流水-通过库存ID查询")
    @ApiOperation(value = "门店库存流水-通过库存ID查询", notes = "门店库存流水-通过库存ID查询")
    @GetMapping(value = "/listUmsMemberSignLogByMemberId")
    public Result<?> listStmStockRecordByStockId(UmsMemberSignLog umsMemberSignLog,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        if (oConvertUtils.isNotEmpty(umsMemberSignLog.getMemberId())) {
            QueryWrapper<UmsMemberSignLog> queryWrapper = QueryGenerator.initQueryWrapper(umsMemberSignLog, req.getParameterMap());
            queryWrapper.orderByDesc("sign_time");
            Page<UmsMemberSignLog> page = new Page<UmsMemberSignLog>(pageNo, pageSize);
			IPage<UmsMemberSignLog> pageList = umsMemberSignLogService.page(page, queryWrapper);
			return Result.ok(pageList);
		} else {
            return Result.error("请选择用户签到记录");
        }
    }

    /**
     * 添加
     *
     * @param umsMemberSign
     * @return
     */
    @AutoLog(value = "会员签到-添加")
    @ApiOperation(value = "会员签到-添加", notes = "会员签到-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody UmsMemberSign umsMemberSign) {
        umsMemberSignService.save(umsMemberSign);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param umsMemberSign
     * @return
     */
    @AutoLog(value = "会员签到-编辑")
    @ApiOperation(value = "会员签到-编辑", notes = "会员签到-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody UmsMemberSign umsMemberSign) {
        umsMemberSignService.updateById(umsMemberSign);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "会员签到-通过id删除")
    @ApiOperation(value = "会员签到-通过id删除", notes = "会员签到-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        umsMemberSignService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "会员签到-批量删除")
    @ApiOperation(value = "会员签到-批量删除", notes = "会员签到-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.umsMemberSignService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "会员签到-通过id查询")
    @ApiOperation(value = "会员签到-通过id查询", notes = "会员签到-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        UmsMemberSign umsMemberSign = umsMemberSignService.getById(id);
        return Result.ok(umsMemberSign);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param umsMemberSign
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, UmsMemberSign umsMemberSign) {
        return super.exportXls(request, umsMemberSign, UmsMemberSign.class, "会员签到");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, UmsMemberSign.class);
    }

}
